/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.auxiliares;

import java.util.List;

import org.grupoavatar.guiaubicua.R;
import org.grupoavatar.guiaubicua.VFiltro;
import org.grupoavatar.guiaubicua.auxiliares.ListaCategoriaAdapter.ListaCategoriaHolder;
import org.grupoavatar.guiaubicua.mix.data.DataSource;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private ExpandableListView mExpandableListView;
	private List<GroupEntity> mGroupCollection;
	private int[] groupStatus;

	public ExpandableListAdapter(Context pContext, ExpandableListView pExpandableListView, List<GroupEntity> pGroupCollection) {
		mContext = pContext;
		mGroupCollection = pGroupCollection;
		mExpandableListView = pExpandableListView;
		groupStatus = new int[mGroupCollection.size()];

		setListEvent();
	}

	private void setListEvent() {

		mExpandableListView
				.setOnGroupExpandListener(new OnGroupExpandListener() {

					@Override
					public void onGroupExpand(int groupPosition) {
						// TODO Auto-generated method stub
						groupStatus[groupPosition] = 1;
					}
				});

		mExpandableListView
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {

					@Override
					public void onGroupCollapse(int groupPosition) {
						// TODO Auto-generated method stub
						groupStatus[groupPosition] = 0;
					}
				});
	}

	@Override
	public String getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).title;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub

		ListaCategoriaHolder childHolder;
		
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.filtro_row, null);

			childHolder = new ListaCategoriaHolder();
			
			childHolder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitleListFiltro);
			childHolder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIconListFiltro);
			childHolder.cbxSel = (CheckBox) convertView.findViewById(R.id.cbxListFiltro);
			convertView.setTag(childHolder);
		}else {
			childHolder = (ListaCategoriaHolder) convertView.getTag();
		}
		
		String nombre;
		if (childPosition != 0)
			nombre = mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).title;
		else
			nombre = "Todos";
		
		childHolder.txtTitle.setText(nombre);
		childHolder.imgIcon.setImageResource(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).icon);
		childHolder.cbxSel
		.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				DataSource.DATASOURCE supracat = DataSource.getSupraCat(VFiltro.categoriasCompletas.get(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1).datasource);
				Item []items = DataSource.getSubCat(supracat);
				if ((isChecked) && (((VFiltro) mContext).getDataSourceChecked().get(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1) == false)) {
					//if (DataSource.esGenerico(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id)) {
					if (childPosition == 0) { //es el de "Todos"
						for (int i=0; i<items.length; i++) {
							((VFiltro) mContext).getDataSourceChecked().set(items[i].id-1, true);
							((VFiltro) mContext).clickOnDataSource(items[i].id-1, true);
						}
						
						notifyDataSetChanged(); //para refrescar la pantalla y se actualice el estado de los checkbox
					} else {
						((VFiltro) mContext).getDataSourceChecked().set(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1, true);
						((VFiltro) mContext).clickOnDataSource(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1, true);
						
						int cuenta = 0;
						for (int j = 1; j < items.length; j++) {
							if (((VFiltro) mContext).getDataSourceChecked().get(items[j].id-1))
								cuenta++;
						}
						
						if (cuenta == items.length-1) { //todos los hijos activos
							((VFiltro) mContext).getDataSourceChecked().set(mGroupCollection.get(groupPosition).GroupItemCollection.get(0).id-1, true);
							((VFiltro) mContext).clickOnDataSource(mGroupCollection.get(groupPosition).GroupItemCollection.get(0).id-1, true);
						} else {
							((VFiltro) mContext).getDataSourceChecked().set(mGroupCollection.get(groupPosition).GroupItemCollection.get(0).id-1, false);
							((VFiltro) mContext).clickOnDataSource(mGroupCollection.get(groupPosition).GroupItemCollection.get(0).id-1, false);
						}
						notifyDataSetChanged(); //para refrescar la pantalla y se actualice el estado de los checkbox
					}
				} else if ((!isChecked)	&& (((VFiltro) mContext).getDataSourceChecked().get(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1) == true)) {
					//((VFiltro) mContext).getDataSourceChecked().set(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1, false);
					//((VFiltro) mContext).clickOnDataSource(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1, false);
					if (childPosition == 0) { //es el de "Todos"
						for (int i=0; i<items.length; i++) {
							((VFiltro) mContext).getDataSourceChecked().set(items[i].id-1, false);
							((VFiltro) mContext).clickOnDataSource(items[i].id-1, false);
						}
						
						notifyDataSetChanged(); //para refrescar la pantalla y se actualice el estado de los checkbox
					} else {
						((VFiltro) mContext).getDataSourceChecked().set(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1, false);
						((VFiltro) mContext).clickOnDataSource(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1, false);
						
						((VFiltro) mContext).getDataSourceChecked().set(mGroupCollection.get(groupPosition).GroupItemCollection.get(0).id-1, false);
						((VFiltro) mContext).clickOnDataSource(mGroupCollection.get(groupPosition).GroupItemCollection.get(0).id-1, false);
						
						notifyDataSetChanged(); //para refrescar la pantalla y se actualice el estado de los checkbox
					}
				}
			}
		});
		
		boolean checked = ((VFiltro) mContext).getDataSourceChecked().get(mGroupCollection.get(groupPosition).GroupItemCollection.get(childPosition).id-1);
		childHolder.cbxSel.setChecked(checked);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return mGroupCollection.get(groupPosition).GroupItemCollection.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mGroupCollection.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mGroupCollection.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		GroupHolder groupHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.filtro_row_general,
					null);
			groupHolder = new GroupHolder();
			groupHolder.img = (ImageView) convertView.findViewById(R.id.imgGeneralIconListFiltro);
			groupHolder.title = (TextView) convertView.findViewById(R.id.txtGenericCatFiltro);
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		/*
		if (groupStatus[groupPosition] == 0) {
			groupHolder.img.setImageResource(R.drawable.group_down);
		} else {
			groupHolder.img.setImageResource(R.drawable.group_up);
		}
		*/
		groupHolder.title.setText(mGroupCollection.get(groupPosition).name);
		groupHolder.img.setImageResource(mGroupCollection.get(groupPosition).icon);
		
		return convertView;
	}

	class GroupHolder {
		ImageView img;
		TextView title;
	}

	class ChildHolder {
		TextView title;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}